package com.icebergteam.warfacewidget.data.sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;

public class CacheTable extends BasicTable {
	public static String className = CacheTable.class.getCanonicalName();
	// Constants
	// Table
	public static final String TABLE_NAME	= "cache";
	// Fields
	public static final String FIELD_ID				= "id ";
	public static final String FIELD_PARENT			= "parent ";
	public static final String FIELD_FILE_URI = "file_uri ";
	public static final String FIELD_TYPE_PARENT = "type_parent ";
	

	public CacheTable() {
		
	}
	
	public String createTableStr() {
		LogManager.LogV(className, "createTable");
		String field_id			= FIELD_ID		+ "integer primary key autoincrement not null";
		String field_parent		= FIELD_PARENT		+ "text";
		String field_file_uri		= FIELD_FILE_URI	+ "text";
		String field_type_parent		= FIELD_TYPE_PARENT	+ "text";
			
		return AppManager.getDatabase().createTableStr(TABLE_NAME, field_id, field_parent, field_file_uri, field_type_parent);
	
	}
	
	public Cursor getCacheByUrl(String url) throws SQLException{
	
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					CacheTable.TABLE_NAME, 
					new String[] { CacheTable.FIELD_PARENT, CacheTable.FIELD_FILE_URI, CacheTable.FIELD_TYPE_PARENT}, 
					String.format("%s = ?", CacheTable.FIELD_PARENT), 
					new String[]{url}, 
					null, 
					null,
					null
					);
			}
		} catch (Exception e) {
			LogManager.LogE(CacheTable.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	public Cursor getCacheByUrl(String parent, String uri) throws SQLException{
		
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					CacheTable.TABLE_NAME, 
					new String[] { CacheTable.FIELD_PARENT, CacheTable.FIELD_FILE_URI, CacheTable.FIELD_TYPE_PARENT}, 
					String.format("%s = ? AND %s = ?", CacheTable.FIELD_PARENT, CacheTable.FIELD_FILE_URI), 
					new String[]{parent, uri}, 
					null, 
					null,
					null
					);
			}
		} catch (Exception e) {
			LogManager.LogE(CacheTable.className, LogManager.getExecuteMethodName(), e.getLocalizedMessage());
			e.printStackTrace();
		}
		return null;
	}
	
//	public long updateOrInsert(ContentValues cv) throws SQLException{
//		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
//		if(db != null && db.isOpen()){
//			 String selection = String.format("%s=? AND %s=? AND %s=?", CacheTable.FIELD_PARENT, CacheTable.FIELD_FILE_URI, CacheTable.FIELD_TYPE_PARENT);
//			 String[] selectionArgs = null;
//
//			 long rowId = 0;
//			 db.beginTransaction();
//			 try {
//			     selectionArgs = new String[] {cv.getAsString(CacheTable.FIELD_PARENT), cv.getAsString(CacheTable.FIELD_FILE_URI), cv.getAsString(CacheTable.FIELD_TYPE_PARENT)};
//
//			     int affected = db.update(CacheTable.TABLE_NAME, 
//			                cv, selection, selectionArgs);
//			      if (affected == 0) {
//			          rowId = db.insert(CacheTable.TABLE_NAME, null, cv);
//			             
//			      }
//			        
//			      db.setTransactionSuccessful();
//			     
//			 } catch (SQLException ex) {
//			      LogManager.LogE(className, LogManager.getExecuteMethodName(),CacheTable.TABLE_NAME,  ex.getLocalizedMessage());
//			      ex.printStackTrace();
//			 } finally {
//			       db.endTransaction();
//			      
//			 }
//			 return rowId;
//		}
//		return -1;
//	}
	
	public void deleteAll() throws SQLException{
		SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
		if(db != null && db.isOpen()){
			db.delete(CacheTable.TABLE_NAME, null, null);
			
		}
	}

	@Override
	protected WhereWrapper getUpdateWhere(ContentValues cv) {
		WhereWrapper where = new WhereWrapper();
		where.setWhere( String.format("%s=? AND %s=? AND %s=?", CacheTable.FIELD_PARENT, CacheTable.FIELD_FILE_URI, CacheTable.FIELD_TYPE_PARENT));
		where.addValue(cv.getAsString(CacheTable.FIELD_PARENT));
		where.addValue(cv.getAsString(CacheTable.FIELD_FILE_URI));
		where.addValue(cv.getAsString(CacheTable.FIELD_TYPE_PARENT));
		return where;     
	}

	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
}